package com.cloudcentral.controller;

import com.cloudcentral.entity.User;
import com.cloudcentral.entity.dto.*;
import com.cloudcentral.entity.query.UserQuery;
import com.cloudcentral.entity.vo.PageVO;
import com.cloudcentral.entity.vo.UserPageVO;
import com.cloudcentral.exception.FailureException;
import com.cloudcentral.service.UserService;
import com.cloudcentral.util.Result;
import com.cloudcentral.util.WebUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 系统用户表 前端控制器
 * </p>
 *
 * @author Administrator
 * @since 2023-10-27
 */
@Slf4j
@RestController
@RequestMapping("/user")
@Tag(name = "用户管理")
public class UserController extends BaseController {
  private final UserService userService;

  @Autowired
  public UserController(UserService userService) {
    this.userService = userService;
  }

  @PostMapping("/enable")
  @Operation(summary = "启用/禁用")
  public Result<Boolean> enable(EnableDTO dto) throws FailureException {
    return Result.ok(userService.enable(dto));
  }

  @PostMapping("/submit")
  @Operation(summary = "提交")
  public Result<Boolean> submit(UserDTO dto) throws Exception {
    return Result.ok(userService.submit(dto, WebUtils.currentUser().getId()));
  }

  @PostMapping("/audit")
  @Operation(summary = "审核")
  public Result<Boolean> audit(UserAuditDTO dto) throws Exception {
    return Result.ok(userService.audit(dto, WebUtils.currentUser().getId()));
  }

  @DeleteMapping("/delete")
  @Operation(summary = "删除")
  public Result<Boolean> delete(Long id) throws Exception {
    return Result.ok(userService.delete(id));
  }

  @GetMapping("/select")
  @Operation(summary = "查询")
  public Result<User> select(Long id) {
    return Result.ok(userService.getById(id));
  }

  @GetMapping("/listPage")
  @Operation(summary = "分页列表")
  public Result<PageVO<UserPageVO>> listPage(UserQuery query) {
    return Result.ok(userService.listPage(query));
  }

  @PostMapping("/resetPwd")
  @Operation(summary = "修改密码")
  public Result<Boolean> resetPwd(ResetPwdDTO dto) throws Exception {
    return Result.ok(userService.resetPwd(dto));
  }

  @PostMapping("/register")
  @Operation(summary = "注册")
  public Result<Boolean> register(RegisterDTO dto) throws Exception {
    return Result.ok(userService.register(dto));
  }
}
